<?php

namespace app\admin\controller\tree\log;

use app\BaseController;
use app\model\FollowUserModel;
use app\model\tree\TreeCollectModel;
use app\model\tree\TreeFruitCommentModel;
use app\model\tree\TreeFruitLikeModel;
use app\model\tree\TreeFruitModel;
use app\model\tree\TreeUserInfoModel;
use app\model\UserModel;
use think\App;

class Index extends BaseController
{

    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 我在智慧树上的个人信息
     * @return bool|string
     */
    public function userInfo(): bool|string
    {
        try {
            $TreeUserInfoModel = new TreeUserInfoModel();
            $info = $TreeUserInfoModel->where('uid','=',$this->userData['id'])->find();
            if ($info){
                $info = $info->toArray();
                if ($info['send_is_nickname'] != 1){
                    $UserModel = new UserModel();
                    $userInfo = $UserModel->field('id,name,avatar')->find($this->userData['id']);
                    $info['nickname'] = $userInfo['name'];
                    $info['avatar'] = $userInfo['avatar'];
                }
            }
            //我收取的果实数量
            $TreeCollectModel = new TreeCollectModel();
            $info['collect_fruit_num'] = $TreeCollectModel->where('uid','=',$this->userData['id'])->count();
            //被我加入关注人群的数量
            $info['collect_fruit_follow_num'] = $TreeCollectModel->where([
                ['uid','=',$this->userData['id']],
                ['is_follow','=',1]
            ])->count();
            //被我申请实名信息的数量
            $info['collect_fruit_real_num'] = $TreeCollectModel->where([
                ['uid','=',$this->userData['id']],
                ['real_name_status','>',0]
            ])->count();
            //我参与回复的数量
            $TreeFruitCommentModel = new TreeFruitCommentModel();
            $info['comment_num'] = $TreeFruitCommentModel->where('uid','=',$this->userData['id'])->count();
            //被我点赞的数量
            $TreeFruitLikeModel = new TreeFruitLikeModel();
            $info['like_num'] = $TreeFruitLikeModel->where('uid','=',$this->userData['id'])->count();
            return success($info);
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 我收取的智慧果
     * @return bool|string
     */
    public function collectList(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'pageSize|每页数量' => 'require',
            ]);
            $TreeCollectModel = new TreeCollectModel();
            $TreeFruitModel = new TreeFruitModel();
            $fruit_id = $TreeFruitModel->where('unit_id','=',$this->userData['unit_id'])->column('id');
            $query = $TreeCollectModel->where([
                ['uid','=',$this->userData['id']],
                ['fruit_id','in',$fruit_id]
            ]);
            $data = $query->with([
                'fruit' => function ($query) {
                    $query->with([
                        'user',
                        'treeUserInfo',
                        'fruitComment' => function ($query) {
                            $query->with(['user','treeUserInfo']);
                        },
                        'fruitImg'
                    ]);
                }
            ])->paginate($param['pageSize'])->toArray();
            $TreeFruitLikeModel = new TreeFruitLikeModel();
            $isLike = $TreeFruitLikeModel->where('uid','=',$this->userData['id'])->column('fruit_id');
            foreach ($data['data'] as &$value) {
                if (in_array($value['fruit_id'], $isLike)) {
                    $value['fruit']['is_like'] = 1;
                }else{
                    $value['fruit']['is_like'] = 0;
                }
            }
            $data['ids'] = $query->column('id');
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 智慧果-查看
     * @return bool|string
     */
    public function info(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'id|ID' => 'require|integer',
            ]);
            $TreeCollectModel = new TreeCollectModel();
            $info = $TreeCollectModel->with([
                'fruit' => function ($query) {
                    $query->with([
                        'user',
                        'treeUserInfo',
                        'fruitComment' => function ($query) {
                            $query->with(['user','treeUserInfo']);
                        },
                        'fruitImg'
                    ]);
                }
            ])->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            $info = $info->toArray();
            $data = [];
            if ($info['fruit']){
                $data = $info['fruit'];
                $data['comment_num'] = count($data['fruitComment']);
                if ($data['is_nickname']){
                    $data['user_name'] = $data['nickname'];
                    $data['avatar'] = $data['nickname_avatar'];
                }
                unset($data['nickname']);
                unset($data['nickname_avatar']);
                $TreeFruitLikeModel = new TreeFruitLikeModel();
                $is_like = $TreeFruitLikeModel->where([
                    ['fruit_id', '=', $data['id']],
                    ['uid','=',$this->userData['id']]
                ])->value('id');
                $data['is_like'] = $is_like ? 1 : 0;
            }
            $data['is_follow'] = $info['is_follow'];
            return success($data);
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 删除收取记录
     * @return bool|string
     */
    public function del(): bool|string
    {
        try {
            $id = $this->request->param('id');
            $TreeCollectModel = new TreeCollectModel();
            if (is_array($id)) {
                $TreeCollectModel->where('id','in',$id)->update([
                    'is_del' => 1,
                    'del_time' => time()
                ]);
            }else{
                $TreeCollectModel->where('id','=',$id)->update([
                    'is_del' => 1,
                    'del_time' => time()
                ]);
            }
            return success();
        } catch (\Exception $e) {
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

//    public function sendList(): bool|string
//    {
//        try {
//            $param = $this->request->param();
//            $this->validate($param,[
//                'pageSize|每页数量' => 'require',
//            ]);
//            $TreeFruitModel = new TreeFruitModel();
//            $data = $TreeFruitModel->where('uid', '=', $this->userData['id'])->with([
//                'user','treeUserInfo','fruitImg'
//            ])->paginate($param['pageSize'])->toArray();
//            foreach ($data['data'] as &$v){
//                if (!empty($v['file']) && !empty($v['fruitImg'])){
//                    $v['icon_status'] = 1;
//                }elseif(!empty($v['file'])){
//                    $v['icon_status'] = 2;
//                }elseif(!empty($v['fruitImg'])){
//                    $v['icon_status'] = 3;
//                }else{
//                    $v['icon_status'] = 0;
//                }
//
//                if ($v['is_nickname']){
//                    $v['user_name'] = $v['nickname'];
//                    $v['avatar'] = $v['nickname_avatar'];
//                }
//                unset($v['nickname']);
//                unset($v['nickname_avatar']);
//            }
//            return success($data);
//        } catch (\Exception $e) {
//            return error($e->getMessage(),$e->getCode(),[
//                'line' => $e->getLine(),
//                'file' => $e->getFile()
//            ]);
//        }
//    }


    /**
     * 申请实名
     * @return bool|string
     */
    public function applyRealName(): bool|string
    {
        $TreeFruitModel = new TreeFruitModel();
        $TreeFruitModel->startTrans();
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'id|ID' => 'require',
                'apply_reason|申请实名原因' => 'require'
            ]);
            $info = $TreeFruitModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            $TreeCollectModel = new TreeCollectModel();
            $collectInfo = $TreeCollectModel->where([
                ['uid','=',$this->userData['id']],
                ['fruit_id','=',$param['id']]
            ])->find();
            if ($collectInfo) {
                $collectInfo->save([
                    'real_name_status' => 1,
                ]);
            }else{
                $TreeCollectModel->insert([
                    'fruit_id' => $param['id'],
                    'uid' => $this->userData['id'],
                    'real_name_status' => 1,
                    'time' => time()
                ]);
            }
            $info->save([
                'real_name_status' => 1,
                'apply_reason' => $param['apply_reason'],
                'apply_uid' => $this->userData['id']
            ]);
            $TreeFruitModel->commit();
            return success();
        } catch (\Exception $e) {
            $TreeFruitModel->rollback();
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 加入关注人群
     * @return bool|string
     */
    public function follow(): bool|string
    {
        $TreeCollectModel = new TreeCollectModel();
        $TreeCollectModel->startTrans();
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'id|ID' => 'require',
            ]);
            $info = $TreeCollectModel->find($param['id']);
            if (!$info) throw new \Exception('参数错误！');
            if ($info['is_follow'] == 1) throw new \Exception('已加入过关注了');
            $info->save([
                'is_follow' => 1
            ]);
            $FollowUserModel = new FollowUserModel();
            $FollowUserModel->insert([
                'unit_id' => $this->userData['unit_id'],
                'uid' => $this->userData['id'],
                'channel' => 10,
                'channel_id' => $param['id'],
                'time' => time(),
            ]);
            $TreeCollectModel->commit();
            return success();
        } catch (\Exception $e) {
            $TreeCollectModel->rollback();
            return error($e->getMessage(),$e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

}